package webServiceClient.academico;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FrmLiberarAtv extends Activity {
	private Button btnAdd;
	private Button btnClose, btnLoad;
	private EditText edtDataLib, edtDataEnd, edtHraLib, edtHraEnd, edtValor;
	private CheckBox cbLiberado;
	private Spinner cbDisciplina, cbTurma, cbAtividade;
	
	private void limpaEdt(){
		edtDataLib.setText("");
		edtDataEnd.setText("");
		edtHraLib.setText("");
		edtHraEnd.setText("");
		edtValor.setText("");
		edtDataLib.requestFocus();
	}
	private void carregaComboAtv(){
		String[] codAtividade;
		try {
			WebServiceClient ws = new WebServiceClient();
			codAtividade = ws.getAtividade("getAtividades",
					Definicoes.getRaAdm(), 
					Definicoes.getSenhaAdm(),
					Definicoes.getSessao(cbDisciplina.getSelectedItem().toString(), "|", 1));
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codAtividade);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbAtividade.setAdapter(adp);
			adp.notifyDataSetChanged();
		} catch (Exception e) {
			Toast t = Toast.makeText(this,
					"Erro ao preencher listas - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}
	private void carregaCombo() {
		String[] codDisciplina;
		try {
			WebServiceClient ws = new WebServiceClient();
			codDisciplina = ws.getDisciplinaProfessor("getDisciplinaProfessor",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
					Definicoes.getRaAdm());
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codDisciplina);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbDisciplina.setAdapter(adp);
		} catch (Exception e) {
			Toast t = Toast.makeText(this,
					"Erro ao preencher listas - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}

		String[] codTurma;
		try {
			WebServiceClient ws = new WebServiceClient();
			codTurma = ws.getDisciplinaProfessor("getTurmasProfessor",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
					Definicoes.getRaAdm());
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codTurma);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbTurma.setAdapter(adp);
		} catch (Exception e) {
			Toast t = Toast.makeText(this,
					"Erro ao preencher listas - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}

	private void carregaBtn() {
		btnLoad = (Button) findViewById(R.FrmLiberarAtv.btnLoad);
		btnAdd = (Button) findViewById(R.FrmLiberarAtv.btnAdd);
		btnClose = (Button) findViewById(R.FrmLiberarAtv.btnClose);
		edtDataEnd = (EditText) findViewById(R.FrmLiberarAtv.edtDataEnd);
		edtDataLib = (EditText) findViewById(R.FrmLiberarAtv.edtDatalib);
		edtHraEnd = (EditText) findViewById(R.FrmLiberarAtv.edtHraEnd);
		edtHraLib = (EditText) findViewById(R.FrmLiberarAtv.edtHraLib);
		edtValor = (EditText) findViewById(R.FrmLiberarAtv.edtValor);
		cbLiberado = (CheckBox) findViewById(R.FrmLiberarAtv.cbLiberar);
		cbDisciplina = (Spinner) findViewById(R.FrmLiberarAtv.cbDisciplina);
		cbTurma = (Spinner) findViewById(R.FrmLiberarAtv.cbTurma);
		cbAtividade = (Spinner) findViewById(R.FrmLiberarAtv.cbAtividade);

		carregaCombo();
		btnLoad.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast t = Toast.makeText(FrmLiberarAtv.this, "Aguarde...", Toast.LENGTH_LONG);
				t.show();
				carregaComboAtv();
			}
		});
		btnAdd.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int lib = 0;
				if (cbLiberado.isChecked() == true)
					lib = 1;
				try {
					WebServiceClient ws = new WebServiceClient();
					String s = ws.setOnAtividade("setOnAtividade", Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
							Definicoes.getSessao(cbAtividade.getSelectedItem().toString(), "|", 1), 
							Definicoes.getSessao(cbDisciplina.getSelectedItem().toString(), "|", 1),
							Definicoes.getSessao(cbTurma.getSelectedItem().toString(), "|", 1), 
							Integer.parseInt(Definicoes.getSessao(cbTurma.getSelectedItem().toString(), "|", 2)), 
							edtDataLib.getText().toString()+" "+ edtHraLib.getText().toString()+":00",
							edtDataEnd.getText().toString() + " "+ edtHraEnd.getText().toString()+":00", lib,
							edtValor.getText().toString());
					if (s.equals("true")){
						Toast t = Toast.makeText(FrmLiberarAtv.this, "Atividade Liberada com secesso", Toast.LENGTH_LONG);
						t.show();
						limpaEdt();
					}else
					{
						Toast t = Toast.makeText(FrmLiberarAtv.this, s, Toast.LENGTH_LONG);
						t.show();					
					}
				} catch (Exception e) {
					Toast t = Toast.makeText(FrmLiberarAtv.this, e.getMessage(), Toast.LENGTH_LONG);
					t.show();
				}
			}
		});

		btnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frm_liberar_atv);
		carregaBtn();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frm_liberar_atv, menu);
		return true;
	}

}
